﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Daycare.DAL;
using Daycare.Models;

namespace Daycare.Services
{
    public class DocumentService
    {
        private DocumentRepository documentRepository;
        private List<DocumentType> documentTypes;
        private List<Document> employeeDocuments;

        public List<DocumentType> GetDocumentTypes()
        {
            documentRepository = new DocumentRepository();
            documentTypes = documentRepository.GetDocumentTypes();

            return documentTypes;
        }

        public List<Document> GetEmployeeDocuments(Employee employee, bool includeExpiredDocuments, DocumentType dType = null)
        {
            documentRepository = new DocumentRepository();
            employeeDocuments = documentRepository.GetEmployeeDocuments(employee, includeExpiredDocuments, dType);

            return employeeDocuments;
        }

        public Document AddDocument(String documentName, String fileName, int documentTypeId, String expirationDate = "")
        {
            documentRepository = new DocumentRepository();
            return documentRepository.AddDocument(documentName, fileName, documentTypeId, expirationDate);
        }

        public void UpdateDocument(Document doc)
        {
            documentRepository = new DocumentRepository();
            documentRepository.UpdateDocument(doc);
        }

        public DocumentType GetDocumentType(int documentTypeId)
        {
            documentRepository = new DocumentRepository();
            return documentRepository.GetDocumentType(documentTypeId);
        }

        public void AddEmployeeDocument(Document document, Employee emp)
        {
            documentRepository = new DocumentRepository();
            documentRepository.AddEmployeeDocument(document, emp);
        }

        public String UploadDocument(String fileName)
        {
            HttpPostedFile file = HttpContext.Current.Request.Files[0];
            String uniqueFileName = fileName + DateTime.Now.ToFileTime() + "." + fileName.Split('.')[1];
            if (file.ContentLength > 0)
            {
                file.SaveAs(HttpContext.Current.Server.MapPath(Configuration.DocumentPath) + uniqueFileName);
                return uniqueFileName;
            }
            else
            {
                return "";
            }
        }

        public void DeleteDocument(int documentId)
        {
            documentRepository = new DocumentRepository();
            documentRepository.DeleteDocument(documentId);
        }

        public Document GetDocument(int documentId)
        {
            documentRepository = new DocumentRepository();

            return documentRepository.GetDocument(documentId);
        }

        public bool DeleteDocumentType(DocumentType docType)
        {
            documentRepository = new DocumentRepository();
            return documentRepository.DeleteDocumentType(docType);
        }

        public bool UpdateDocumentType(DocumentType docType)
        {
            documentRepository = new DocumentRepository();
            return documentRepository.UpdateDocumentType(docType);
        }

        public bool AddDocumentType(String documentName)
        {
            documentRepository = new DocumentRepository();
            return documentRepository.AddDocumentType(documentName);
        }
    }
}